const Koa = require('koa');
const app = new Koa();

//接收post请求参数
const bodyParser = require('koa-bodyparser');

//路由
const Router = require('koa-router');

// 导入controller
const controller = require('./controller');
var cors = require('koa2-cors');

//路径
const path = require('path')
let PUBLIC_PATH = path.resolve(__dirname);
const koaBody = require('koa-body')

//接收文件上传
const upload = require('./upload')
const sendUpload = require('./sendImgUpload')

//访问静态资源
const staticFiles = require('koa-static')
app.use(staticFiles(path.join(__dirname,'assets')))

//token校验
const checkToken = require('./checkToken.js')
app.use(checkToken)


//上传图片的接口集合
var db = new Router()
//商品上传图片接口
db.post('/upload/goods_img',upload.array('file',1),async(ctx)=>{
  const files = ctx.req.files[0]; //上传过来的文件
  console.log("图片",files)
  ctx.body = {
    data:'http://120.24.225.30/img/'+ files.filename,  //http://120.24.225.30/img/
    msg:'添加成功'
  }
})

//文章图片接口
db.post('/upload/article_img',upload.array('file',1),async(ctx)=>{
  const files = ctx.req.files[0]; //上传过来的文件
  console.log("图片",files)
  ctx.body = {
    data:'http://120.24.225.30/img/'+ files.filename,  //http://10.78.210.47:8081/img/
    msg:'添加成功'
  }
})

//送货图片上传
db.post('/upload/sendImg',sendUpload.array('file',1),async(ctx)=>{
  const files = ctx.req.files[0]; //上传过来的文件
  ctx.body = {
    data:'http://120.24.225.30/sendImg/'+ files.filename,  //http://120.24.225.30/img/
    msg:'添加成功'
  }
})


app.use(cors());
app.use(db.routes());
app.use(bodyParser());
app.use(controller());
app.listen('8081')
